module.exports = (sequelize, DataTypes) => {
  const FamilyMember = sequelize.define('FamilyMember', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    user_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '关联用户ID'
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: '成员姓名'
    },
    avatar: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: '头像URL'
    },
    gender: {
      type: DataTypes.ENUM('male', 'female', 'other'),
      defaultValue: 'other',
      comment: '性别'
    },
    birthday: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: '出生日期'
    },
    age: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '年龄'
    },
    height: {
      type: DataTypes.FLOAT,
      allowNull: true,
      comment: '身高(cm)'
    },
    weight: {
      type: DataTypes.FLOAT,
      allowNull: true,
      comment: '体重(kg)'
    },
    blood_type: {
      type: DataTypes.ENUM('A', 'B', 'AB', 'O', 'unknown'),
      defaultValue: 'unknown',
      comment: '血型'
    },
    relationship: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: '与用户的关系'
    },
    medical_history: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '病史'
    },
    allergies: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '过敏史'
    },
    medications: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '用药情况'
    },
    health_status: {
      type: DataTypes.ENUM('normal', 'warning', 'abnormal'),
      defaultValue: 'normal',
      comment: '健康状态'
    }
  }, {
    tableName: 'family_members',
    timestamps: true,
    paranoid: true,
    hooks: {
      beforeCreate: (member) => {
        // 如果提供了生日，计算年龄
        if (member.birthday && !member.age) {
          const today = new Date();
          const birthDate = new Date(member.birthday);
          let age = today.getFullYear() - birthDate.getFullYear();
          const monthDiff = today.getMonth() - birthDate.getMonth();
          
          if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
            age--;
          }
          
          member.age = age;
        }
      },
      beforeUpdate: (member) => {
        // 如果更新了生日，重新计算年龄
        if (member.changed('birthday')) {
          const today = new Date();
          const birthDate = new Date(member.birthday);
          let age = today.getFullYear() - birthDate.getFullYear();
          const monthDiff = today.getMonth() - birthDate.getMonth();
          
          if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
            age--;
          }
          
          member.age = age;
        }
      }
    }
  });

  FamilyMember.associate = function(models) {
    FamilyMember.belongsTo(models.User, {
      foreignKey: 'user_id',
      as: 'user'
    });
    
    FamilyMember.hasMany(models.HealthRecord, {
      foreignKey: 'member_id',
      as: 'healthRecords'
    });
    
    FamilyMember.hasMany(models.HealthReminder, {
      foreignKey: 'member_id',
      as: 'healthReminders'
    });
    
    FamilyMember.hasMany(models.HealthReport, {
      foreignKey: 'member_id',
      as: 'healthReports'
    });
  };

  return FamilyMember;
}; 